<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理</title>
    <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="css/style.winning.css?v=1.0.0" rel="stylesheet">
    <link rel="stylesheet" href="css/metroStyle/metroStyle.css" type="text/css">
    <link rel="stylesheet" href="css/plugins/table/bootstrap-table.css" type="text/css">
    <link href="css/plugins/iCheck/custom.css?v=1.0.0" rel="stylesheet">
</head>

<body>

<div class="mr2" style="padding:5px 0px 0px 20px;overflow:auto;height: 360px">
        <div class="input-group">
            <input  type="hidden" placeholder="输入机构名称检索" class="form-control" id="key">
        </div>
        <div>
            <div class="zTreeDemoBackground left">
                <div id="tree_jg" class="ztree"></div>
            </div>
        </div>
</div>


<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript" src="js/content.min.js?v=1.0.0"></script>
<script type="text/javascript" src="js/jquery.ztree.core.js?v=3.5"></script>
<script type="text/javascript" src="js/jquery.ztree.exhide.js?v=1.0.0"></script>
<script type="text/javascript" src="js/fuzzysearch.js?v=1.0.0"></script>
<script type="text/javascript" src="js/plugins/iCheck/icheck.min.js"></script>
<script type="text/javascript" src="js/plugins/layer/layer.min.js?v=1.0.0"></script>
<script type="text/javascript" src="platcommon/layer-wn.js?v=1.0.0"></script>
<script type="text/javascript" src="platcommon/common.js?v=1.0.0"></script>

<script>
    var paramId = "";
    var paramName = "";
    var cdmc="";
    $(function () {
        $.fn.zTree.init($("#tree_jg"), setting, [{}]);
        getJgTree();
        // fuzzySearch('tree_jg', '#key', null, true); //初始化模糊搜索方法
        paramId = Common.GetRequest().paramId;
        paramName = Common.GetRequest().paramName;
        cdmc = Common.GetRequest().cdmc;
    });

    //**********************机构树形开始
    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false,
            showIcon: false

        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: jgTreeOnClick,
            onExpand: zTreeOnExpand
        }

    };
    //***********************机构树形end

    /**
     * 定义树节点展开事件，查询子节点
     * @param event
     * @param treeId
     * @param treeNode
     */
    function zTreeOnExpand(event, treeId, treeNode){
        queryChildrenList(treeNode);
    }

    /**
     * 查询子节点
     * @param treeNode
     */
    function queryChildrenList(treeNode){
        var jgmc = $("#key").val();
        if(jgmc != '' || treeNode.children != undefined){
            return;
        }
        var queryParms = {
            pid : treeNode.id,
            jgmc : jgmc
        };
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "commonapi/findjgtreeByPid",
            data: queryParms,
            success:function(data){
                //添加子节点到指定的父节点
                var jsondata= eval(data.jgTreeList);
                for(var i=0;i<jsondata.length;i++){
                    if(jsondata[i].isLeaf != '1'){
                        jsondata[i].isParent = true;
                    }
                }
                if(jsondata != null && jsondata != ""){
                    var treeObj = $.fn.zTree.getZTreeObj("tree_jg");
                    var parentZNode = treeObj.getNodeByParam("id", treeNode.id, null);
                    treeObj.addNodes(parentZNode,jsondata, false);
                }
            },
            error: function () {
                alert('请求失败');
            }
        });
    }

    //***************************机构  ztree 点击样式 *************
    var newCount = 1;
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click",
            function () {
                var zTree = $.fn.zTree.getZTreeObj("tree_jg");
                zTree.addNodes(treeNode, {
                    id: (100 + newCount),
                    pId: treeNode.id,
                    name: "new node" + (newCount++)
                });
                return false;
            });
    };

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };

    //***************************机构 ztree 点击样式 *************
    /**
     * 获取当前机构树形
     */
    function getJgTree() {
        var jgmc = $('#key').val();
        var param = {jgmc : jgmc};
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "commonapi/findjgtree",
            data: param,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    //$.fn.zTree.init($("#tree_jg"), setting, "正在查询");
                    var jgTreeList = jsonData.jgTreeList;
                    for(var i=0;i<jgTreeList.length;i++){
                        if(jgTreeList[i].isLeaf != '1'){
                            jgTreeList[i].isParent = true;
                        }
                    }
                    $.fn.zTree.init($("#tree_jg"), setting, jgTreeList);
                } else {
                }
            }
        });
    }

    //树 单击事件
    function jgTreeOnClick(event, treeId, treeNode) {
        if(treeNode.oldname==null){
            $(win_obj).find('#' + paramName).val(treeNode.name);
        }else{
            $(win_obj).find('#' + paramName).val(treeNode.oldname);
        }

        $(win_obj).find('#' + paramId).val(treeNode.id);
        var index = parent.layer.getFrameIndex(window.name);
        //cdmc 为机构用户管理  则触发事件
        if(cdmc != null  || cdmc !=""){
            for(var k=0;k<parent.frames.length;k++){
                if(parent.frames[k].frameElement.src.indexOf("plat-user.html")!=-1)
                    parent.frames[k].getRoleByJgbm();
            }
         }

        setTimeout(function () {
            parent.layer.close(index)
        }, 1000);
    }
    var win_obj;
    function initView(object) {
        win_obj = object;
    }
</script>
</body>
</html>